/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-8-19 下午1:59 Created by lyyang
 */
package com.jade.bss.web.security;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.apache.shiro.subject.Subject;

import java.util.Collection;
import java.util.Iterator;

/**
 * session监听器
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-8-19 下午1:59
 */
public class LimitUserSessionListener implements SessionListener
{
    protected Log logger = LogFactory.getLog(this.getClass());

    private PrincipalUUIDGenerator uuidGenerator;

    public PrincipalUUIDGenerator getUuidGenerator()
    {
        return uuidGenerator;
    }

    public void setUuidGenerator(PrincipalUUIDGenerator uuidGenerator)
    {
        this.uuidGenerator = uuidGenerator;
    }

    public void onStart(Session session)
    {
        Subject subject = SecurityUtils.getSubject();
        logger.debug("==onStart===" + subject.getPrincipal());
        Collection keys = session.getAttributeKeys();
        if (keys!=null&&keys.size()>0){
            for (Iterator it=keys.iterator();it.hasNext();){
                Object key = it.next();
                logger.debug(key.toString()+":"+session.getAttribute(key));
            }
        }
    }

    public void onStop(Session session)
    {
        logger.debug("==onStop===1");
        Collection keys = session.getAttributeKeys();
        if (keys!=null&&keys.size()>0){
            for (Iterator it=keys.iterator();it.hasNext();){
                Object key = it.next();
                logger.debug(key.toString()+":"+session.getAttribute(key));
            }
        }
        logger.debug("==onStop===2");
    }

    public void onExpiration(Session session)
    {
        logger.info("==onExpiration===1");
        Collection keys = session.getAttributeKeys();
        if (keys!=null&&keys.size()>0){
            for (Iterator it=keys.iterator();it.hasNext();){
                Object key = it.next();
                logger.debug(key.toString()+":"+session.getAttribute(key));
            }
        }
        logger.info("==onExpiration===2");
    }
}
